const { MOCK } = require('../utils').settings
const axios = require('../utils/axios')
const { error } = require('../utils/error')
const { mockBanner, mockNewsList, mockSearch } = require('./mock/public')
const apiError = (msg) => {
  error('50000', __dirname, msg)
}
// public - banner
exports.requestBanner = async () => {
  let result = MOCK ? mockBanner : await axios.get('/getTopLineHome')
  if (!result) {
    apiError('requestBanner')
    result = mockBanner
  }
  return result
}

// public - newsList
exports.requestNewsList = async (data = {}) => {
  let result = MOCK
    ? mockNewsList
    : await axios.get('/getNewsByClass', {
        params: data
      })
  // let result = await axios.get('/getNewsByClass', {
  // params: data
  // })
  if (!result) {
    apiError('requestNewsList')
    result = mockNewsList
  }
  return result
}
// public - search
exports.requestSearch = async (data = {}) => {
  let result = MOCK
    ? mockSearch
    : await axios.get('/search', {
        params: data
      })
  // let result = await mockSearch
  if (!result) {
    apiError('requestSearch')
    result = mockSearch
  }
  return result
}
// public - IPAddress
exports.requestIPAddress = async (ip = '127.0.0.1') => {
  let result = await axios.get('http://ip.taobao.com/service/getIpInfo.php', {
    params: {
      ip: ip === '127.0.0.1' ? 'myip' : ip
    }
  })
  if (!result) {
    apiError('requestIPAddress')
    result = {
      code: 0,
      data: {
        ip: '127.0.0.1',
        country: 'XX',
        area: '',
        region: 'XX',
        city: '内网IP',
        county: '内网IP',
        isp: '内网IP',
        country_id: 'xx',
        area_id: '',
        region_id: 'xx',
        city_id: 'local',
        county_id: 'local',
        isp_id: 'local'
      }
    }
  }
  return result
}
